package com.jindianjichi.subject.application.controller;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jindianjichi.subject.application.convert.SubjectCategoryDtoConverter;
import com.jindianjichi.subject.application.convert.SubjectLabelDtoConverter;
import com.jindianjichi.subject.application.entity.SubjectCategoryDto;
import com.jindianjichi.subject.application.entity.SubjectLabelDto;
import com.jindianjichi.subject.common.entity.Result;
import com.jindianjichi.subject.domain.convert.SubjectCategoryConverter;
import com.jindianjichi.subject.domain.convert.SubjectLabelConvert;
import com.jindianjichi.subject.domain.entity.SubjectCategoryBo;
import com.jindianjichi.subject.domain.service.SubjectCategoryDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/subject/category")
public class SubjectCategoryController {
    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    @PostMapping("/add")
    public Result add(@RequestBody SubjectCategoryDto subjectCategoryDto) {
        try {
            boolean infoEnabled = log.isInfoEnabled();
            log.info(String.valueOf(infoEnabled));
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.add.dto:{}", JSON.toJSONString(subjectCategoryDto));
            }
            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConverter.instance.convertToBoCategory(subjectCategoryDto);
            subjectCategoryDomainService.addCategory(subjectCategoryBo);
            return Result.ok(true);
        } catch (Exception e) {
            return Result.fail("新增题目失败");
        }


    }

    /**
     * 查询岗位大类
     */
    @PostMapping("/queryPrimaryCategory")
    public Result<SubjectCategoryDto> queryPrimaryCategory(@RequestBody SubjectCategoryDto subjectCategoryDto) {
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryController.add.dto:{}", JSON.toJSONString(subjectCategoryDto));
        }
        try {
            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConverter.instance.convertToBoCategory(subjectCategoryDto);
            List<SubjectCategoryBo> boList = subjectCategoryDomainService.queryPrimaryCategory(subjectCategoryBo);
            List<SubjectCategoryDto> subCategoryList = SubjectCategoryDtoConverter.instance.convertBoToCategory(boList);
            return Result.ok(subCategoryList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.fail("查询失败");
        }
    }

    @PostMapping("/delete")
    public Result deleteCayegory(@RequestBody SubjectCategoryDto subjectCategoryDto) {
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryController.add.dto:{}", JSON.toJSONString(subjectCategoryDto));
        }
        try {
            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConverter.instance.convertToBoCategory(subjectCategoryDto);
            int deleteCategoryNum = subjectCategoryDomainService.deleteCategory(subjectCategoryBo);
            Result result = new Result<>();
            result.setCode(200);
            result.setMessage("删除成功" + deleteCategoryNum + "条数据");
            result.setData(deleteCategoryNum);
            return result;
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.fail("删除失败");
        }
    }


    @PostMapping("/update")
    public Result<Boolean> updateCayegory(@RequestBody SubjectCategoryDto subjectCategoryDto) {
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryController.add.dto:{}", JSON.toJSONString(subjectCategoryDto));
        }
        try {
            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConverter.instance.convertToBoCategory(subjectCategoryDto);
            Boolean ifSuccess = subjectCategoryDomainService.updateCategory(subjectCategoryBo);
            return Result.ok(ifSuccess);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.fail("删除失败");
        }
    }


    //根据大的分类id查询所有的小分类
    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDto>> queryCategoryByPrimary(@RequestBody SubjectCategoryDto subjectCategoryDTO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryController.add.dto:{}", JSON.toJSONString(subjectCategoryDTO));
        }
        try {
          Preconditions.checkNotNull(subjectCategoryDTO.getParentId(),"分类查询的id不能为空");
            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConverter.instance.convertToBoCategory(subjectCategoryDTO);
            List<SubjectCategoryBo> list=subjectCategoryDomainService.queryCategory(subjectCategoryBo);
            List<SubjectCategoryDto> dtoList = SubjectCategoryDtoConverter.instance.convertBoToCategory(list);
//        SubjectCategoryDtoConverter.instance.convertBoToCategory(list);
            return Result.ok(dtoList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.fail("删除失败");
        }
    }

    /**
     * 查询分类及标签一次性
     * @param subjectCategoryDTO
     * @return
     */

    //根据大的分类id查询所有的小分类
    @PostMapping("/queryCategoryAndLabel")
    public Result<List<SubjectCategoryDto>> queryCategoryAndLabel(@RequestBody SubjectCategoryDto subjectCategoryDTO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryController.queryCategoryAndLabel.dto:{}", JSON.toJSONString(subjectCategoryDTO));
        }
        try {
            Preconditions.checkNotNull(subjectCategoryDTO.getId(),"分类查询的id不能为空");
            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConverter.instance.convertToBoCategory(subjectCategoryDTO);
            List<SubjectCategoryBo> list=subjectCategoryDomainService.queryCategoryAndLabel(subjectCategoryBo);
            List<SubjectCategoryDto> dtoList = new LinkedList<>();
            for (SubjectCategoryBo bo:list){
               SubjectCategoryDto dto=SubjectCategoryDtoConverter.instance.convertBoToCategoryDto(bo);
                List<SubjectLabelDto> labelDtoList = SubjectLabelDtoConverter.instance.convertToDtoLabel(bo.getLabelBoList());
                dto.setLabelDtoList(labelDtoList);
                dtoList.add(dto);
            }

            return Result.ok(dtoList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.fail("查询失败");
        }
    }
}
